<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务类别" prop="serviceType">
        <el-select
          v-model="queryParams.serviceType"
          placeholder="服务类别"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="服务状态" prop="serviceState">
        <el-select
          v-model="queryParams.serviceState"
          placeholder="服务状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions2"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fuw:service:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fuw:service:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fuw:service:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fuw:service:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务编号" align="center" prop="serviceId" />
      <el-table-column label="服务名称" align="center" prop="serviceName" :show-overflow-tooltip='true'/>
      <el-table-column label="医务人员编号" align="center" prop="medicalId" :show-overflow-tooltip='true'/>
      <el-table-column label="用户/患者编号" align="center" prop="personId" :show-overflow-tooltip='true'/>
      <el-table-column label="医疗服务种类" align="center" prop="serviceType" />
      <el-table-column label="医疗服务内容" align="center" prop="serviceText" :show-overflow-tooltip='true'/>
      <el-table-column label="医疗服务状态" align="center" prop="serviceState" />
      <el-table-column label="医疗服务反馈" align="center" prop="serviceFeedback" :show-overflow-tooltip='true'/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetails(scope.row)"
            v-hasPermi="['fuw:service:details']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fuw:service:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fuw:service:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医疗服务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" maxlength="20" show-word-limit/>
        </el-form-item>

        <el-form-item label="医务人员编号" prop="medicalId">
          <el-select
            v-model="form.medicalId"
            placeholder="请选择医务人员"
            style="width: 280px">
            <el-option
              v-for="item in medicalList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户编号" prop="personId">
          <el-select
            v-model="form.personId"
            placeholder="请选择用户"
            style="width: 280px">
            <el-option
              v-for="item in personList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务类别" prop="serviceType">
          <el-select
            v-model="form.serviceType"
            placeholder="服务类别"
            clearable
            size="small"
            style="width: 280px"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="医疗服务内容" prop="serviceText">
          <el-input v-model="form.serviceText" type="textarea" :autosize="{ minRows: 5}" placeholder="请输入内容" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="医疗服务状态" prop="serviceState">
          <el-steps :active="form.serviceState" finish-status="success">
            <el-step title="发起"></el-step>
            <el-step title="接收"></el-step>
            <el-step title="完成"></el-step>
            <el-step title="反馈"></el-step>
          </el-steps>
          <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
        </el-form-item>
        <el-form-item label="医疗服务反馈" prop="serviceFeedback">
          <el-input v-model="form.serviceFeedback" type="textarea" :autosize="{ minRows: 5}" placeholder="请输入内容" maxlength="100" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 医疗服务详情对话框 -->
    <el-dialog :title="title" :visible.sync="openNow" width="880px" append-to-body>
      <el-descriptions class="margin-top" title="" :column="3" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            服务名称
          </template>
          {{form.serviceName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            医疗人员编号
          </template>
          {{form.medicalId}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            用户编号
          </template>
          {{form.personId}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            服务类别
          </template>
          <!-- （1：身体恢复；2：辅助治疗；3：居家养病；4：残疾服务；） -->
          <el-tag size="small" v-if="form.serviceType == 1">身体恢复</el-tag>
          <el-tag size="small" v-else-if="form.serviceType == 2">辅助治疗</el-tag>
          <el-tag size="small" v-else-if="form.serviceType == 3">居家养病</el-tag>
          <el-tag size="small" v-else-if="form.serviceType == 4">残疾服务</el-tag>
          <el-tag size="small" type="danger" v-else>未识别</el-tag>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-office-building"></i>
            医疗服务内容
          </template>
          {{form.serviceText}}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-office-building"></i>
            医疗服务状态
          </template>
          <el-steps :active="form.serviceState" finish-status="success">
            <el-step title="发起"></el-step>
            <el-step title="接收"></el-step>
            <el-step title="完成"></el-step>
            <el-step title="反馈"></el-step>
          </el-steps>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-office-building"></i>
            医疗服务反馈
          </template>
          {{form.serviceFeedback}}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
import { listService, getService, listPerson, listMedical, delService, addService, updateService, exportService } from "@/api/fuw/service";

export default {
  name: "Service",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医疗服务表格数据
      serviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openNow: false,
      // 服务类别数据字典
      statusOptions: [],
      // 服务状态数据字典
      statusOptions2: [],
      // 下拉框的2个数组
      personList: [],
      medicalList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceName: null,
        serviceType: null,
        serviceState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("fuw_service_type").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("fuw_service_state").then(response => {
      this.statusOptions2 = response.data;
    });
    // 查询下拉框数据
    this.getListByTabId();
  },
  methods: {
    /** 步骤条下一步 */
    next() {
      if (this.form.serviceState++ > 3) this.form.serviceState = 0;
    },
    /** 查询下拉框列表 */
    getListByTabId() {
      listPerson().then(response => {
        this.personList = response.data;
      });
      listMedical().then(response => {
        this.medicalList = response.data;
      });
    },
    /** 查询医疗服务列表 */
    getList() {
      this.loading = true;
      listService(this.queryParams).then(response => {
        this.serviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openNow = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serviceId: null,
        serviceName: null,
        medicalId: null,
        personId: null,
        serviceType: null,
        serviceText: null,
        serviceState: null,
        serviceFeedback: null
      };
      this.personList = [],
      this.medicalList = [],
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 查询下拉框数据
      this.getListByTabId();
      this.reset();
      this.open = true;
      this.title = "添加医疗服务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // 查询下拉框数据
      this.getListByTabId();
      this.reset();
      const serviceId = row.serviceId || this.ids
      getService(serviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改医疗服务";
      });
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      // 查询下拉框数据
      this.getListByTabId();
      this.reset();
      const serviceId = row.serviceId || this.ids
      getService(serviceId).then(response => {
        this.form = response.data;
        this.openNow = true;
        this.title = "医疗服务详情";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serviceId != null) {
            updateService(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addService(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serviceIds = row.serviceId || this.ids;
      this.$confirm('是否确认删除医疗服务编号为"' + serviceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delService(serviceIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有医疗服务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportService(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
